﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Nova_DAL;

public partial class Search_THistory : System.Web.UI.Page
{
    string strSQL = "";
    DbHelper access = new DbHelper();
    DbCommand cmd = null;
    decimal SumCost = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //gv_Bind();
        }
    }

    /// <summary>
    /// 查询语句
    /// </summary>
    /// <returns></returns>
    private void gv_Bind()
    {
        string WCode = Request.Cookies["WType"].Value;
        //        strSQL = @"select case THS.TaskMode when 70 then 'WMS直接添加库存' when 80 then 'WMS直接删除库存' else ECST.WMSTypeName end as WMSTypeName,
        //                    THS.TaskMode,THS.CurPos_ID,THS.TarPos_ID,THS.Sec_Mat_ID,THS.Mat_Num,THS.XType,
        //                    TBI.Mat_Code,TBM.Mat_Name,TRP.Pos_ID,THS.CDT,TBI.S02,TBI.S03,TBI.S04,TBI.S05,TBS.CDT,TBC.CustomName,
        //                    TBI.S06,TBI.S07,TBI.S08,TBI.S09,TBS.MAT_Num*ISNULL(CAST(TBI.S09 AS int),1) as ZS10,TBI.S11,TBC.CustomName,
        //                    TBI.S12,TBI.S13,TBI.S14,THS.Mat_Num,THS.OID
        //                    from THistory THS 
        //                    left join ECSStandard..TSQModeRelations ECST on THS.TaskMode= ECST.WMSType
        //                    left join TBase_Interface TBI ON TBI.Sec_Mat_ID = THS.Sec_Mat_ID
        //                    LEFT JOIN TBase_Material TBM on TBM.Mat_Code = TBI.Mat_Code
        //                    left join TBase_Storage TBS on TBS.Sec_Mat_ID = THS.Sec_Mat_ID
        //                    LEFT JOIN TRel_TrayPosContainer TTP ON TTP.ContainerID = TBS.ContainerID
        //                    LEFT JOIN TBase_TrayPos TBT ON TBT.Relations_ID = TTP.Relations_ID
        //                    LEFT JOIN TBCustom TBC ON TBC.CustomNo = TBI.S13
        //                    LEFT JOIN TRel_PosTray TRP ON TRP.Tray_ID = TBT.Tray_ID where 1=1";
        strSQL = @"select top 100 case isnull(ECST.WMSTypeName,'') when '' then THS.TaskModeName else ECST.WMSTypeName end as WMSTypeName,THS.PID,
                    THS.TaskMode,THS.CurPos_ID,THS.TarPos_ID,THS.Sec_Mat_ID,THS.Mat_Num,THS.XType,
                    TBI.Mat_Code,TBM.Mat_Name,THS.CDT,TBI.S02,TBI.S03,TBI.S04,TBI.S05,THS.CDT,
                    TBI.S06,TBI.S07,TBI.S08,TBI.S09,THS.MAT_Num*(ISNULL(CAST(TBI.S09 AS int),0)) as ZS10,TBI.S11,
                    TBI.S12,TBI.S13,TBI.S14,THS.Mat_Num,THS.OID,THS.Tray_ID
                    from THistory THS 
                    left join ECSStandard..TSQModeRelations ECST on THS.TaskMode= ECST.WMSType
                    left join TBase_Interface TBI ON TBI.Sec_Mat_ID = THS.Sec_Mat_ID
                    LEFT JOIN TBase_Material TBM on TBM.Mat_Code = TBI.Mat_Code 
                    where 1=1 and  (TBI.S01='" + WCode + "' or 'FSSC'='" + WCode + "')";
        String tempStr = "";

        if (txtMat_Code.Text != "")
        {
            tempStr += " and TBI.Mat_Code ='" + txtMat_Code.Text + "'";
        }

        //if (txtS03.Text != "")
        //{
        //    tempStr += " and TBI.S03 = '" + txtS03.Text.ToString().Trim() + "'";
        //}


        if (Hidden1.Value != "")
        {
            tempStr += " and TBI.S13 = '" + Hidden1.Value.ToString() + "'";
        }

        if (ddlTaskMode.SelectedValue != "")
        {
            tempStr += " and THS.TaskMode = '" + ddlTaskMode.SelectedValue.ToString() + "'";
        }

        if (txtCDTS.Text.Trim() != "")//创建时间-开始
        {
            tempStr += " AND THS.[CDT]>='" + txtCDTS.Text.Trim() + "'";
        }
        if (txtCDTE.Text.Trim() != "")//创建时间-结束
        {
            tempStr += " AND THS.[CDT]<='" + txtCDTE.Text.Trim() + "'";
        }
        strSQL = strSQL + tempStr;
        strSQL += " order by THS.CDT desc";
        cmd = access.GetSqlStringCommond(strSQL);
        gv.DataSource = access.ExecuteDataSet(cmd);
        gv.DataBind();

        strSQL = "";
        strSQL = @"select Sum(THS.Mat_Num)
                    from THistory THS 
                    left join ECSStandard..TSQModeRelations ECST on THS.TaskMode= ECST.WMSType
                    left join TBase_Interface TBI ON TBI.Sec_Mat_ID = THS.Sec_Mat_ID
                    LEFT JOIN TBase_Material TBM on TBM.Mat_Code = TBI.Mat_Code
                    left join TBase_Storage TBS on TBS.Sec_Mat_ID = THS.Sec_Mat_ID
                    LEFT JOIN TRel_TrayPosContainer TTP ON TTP.ContainerID = TBS.ContainerID
                    LEFT JOIN TBase_TrayPos TBT ON TBT.Relations_ID = TTP.Relations_ID
                    LEFT JOIN TRelations TRP ON TRP.Tray_ID = TBT.Tray_ID where 1=1 and  (TBI.S01='" + WCode + "' or 'FSSC'='" + WCode + "')";
        strSQL = strSQL + tempStr;
        String toTalNum = "0";
        cmd = access.GetSqlStringCommond(strSQL);
        toTalNum = access.ExecuteScalar(cmd).ToString();
        if (toTalNum == "")
        {
            toTalNum = "0";
        }
        lblTotal.Text = toTalNum;

    }

    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        lblCost.Text = "0";
        gv_Bind();
    }

    protected void ibtnReSet_Click(object sender, ImageClickEventArgs e)
    {
        txtMat_Code.Text = "";
        txtMat_Name.Text = "";
        //txtS01.Text = "";
        Hidden1.Value = "";
    }

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        lblCost.Text = "";
        if (e.Row.RowIndex != -1)
        {
            if (e.Row.Cells[8].Text == "0")
            {
                e.Row.Cells[8].Text = "箱";
            }
            else if (e.Row.Cells[8].Text == "1")
            {
                e.Row.Cells[8].Text = "袋";
            }
            else
            {
                e.Row.Cells[8].Text = "桶";
            }
            if (e.Row.Cells[9].Text == "" || e.Row.Cells[9].Text == null || e.Row.Cells[9].Text.Trim() == "&nbsp;")
            {
                e.Row.Cells[9].Text = "0";
            }
            else
            {
                ////SumCost = SumCost + ParseInt32(e.Row.Cells[12].Text.ToString());
                // SumCost = SumCost + Convert.ToInt32(e.Row.Cells[12].Text.ToString());
                SumCost += decimal.Parse(e.Row.Cells[9].Text);

                //SumCost = SumCost + e.Row.Cells[13].Text;
            }
        }
        lblCost.Text = SumCost.ToString();

    }


    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv.PageIndex = e.NewPageIndex;
        gv_Bind();  //重新绑定GridView
    }
}
